#------------------------------------------------------------------------------
# iosMailAccounts.pl
#   Plugin for MacRipper
#   Ipad email account settings
#
# Change history
#   20120506 [fpi] * first version
#
# References
# 
# copyright 2012 Francesco Picasso <francesco.picasso@gmail.com>
#------------------------------------------------------------------------------

package iosMailAccounts;
use strict;

#------------------------------------------------------------------------------

my %config = (
    version         => 20120506,
    hasDescr        => 1,
    hasRefs         => 0,
    applyto			=> 'ios',
    srctype			=> "plist",
    srcfile		    => "com.apple.accountsettings.plist",
    srcpath		    => "/mobile/Library/Preferences/"
);

sub getConfig       { return %config; }
sub getDescription  { return "Ipad email account settings"; }
sub getReferences   {}
sub getSourcePath   { return $config{srcpath}; }
sub getSourceFile   { return $config{srcfile}; }
sub getSourceType   { return $config{srctype}; }
sub getVersion      { return $config{version}; }
sub getApplyTo      { return $config{applyto}; }

#------------------------------------------------------------------------------
# helpers

my $VERSION = getVersion();
my $tab;
my $tab0 = '';
my $tab1 = '  ';
my $tab2 = $tab1.$tab1;
my $tab3 = $tab1.$tab1.$tab1;
my $noalign = "%s";
my $align10 = "%-10s";
my $align15 = "%-15s";
my $align20 = "%-20s";
my $align25 = "%-25s";

#------------------------------------------------------------------------------

sub pluginmain {
	my $class = shift;
	my $plist = shift;
    my $tzone = shift;
	::logMsg("Launching iosMailAccounts v.".$VERSION);
    die "Invalid PLIST object" if ( not defined $plist );
    
    #use Data::Dumper; print Dumper($plist); return;
    
    my $accountsKey = 'Accounts';
    my $accounts = ::getPlistKeyValue( $plist, $accountsKey );
    if ( not defined $accounts ) { ::rptMsg("[$accountsKey] not found"); return; }
    
    my $dictName;
    my $dictVal;
    for (@{$accounts}) {
        $dictName = 'Class';
        $dictVal = ::getPlistKeyValue( $_, $dictName );
        if ( not defined $dictVal ) {
            ::rptMsg( "WARNING: plist file contains data not recognized by plugin" );
            next;
        }
        ::rptMsg( "Class: $dictVal" );      
        if ( $dictVal eq 'DeviceLocalAccount' ) {
            parseDeviceLocalAccount( $_, $tab1, $align25 );
        }
        elsif ( $dictVal eq 'LocalAccount' ) {
            parseLocalAccount( $_, $tab1, $align25 );
        }
        elsif ( $dictVal eq 'SMTPAccount' ) {
            parseSMTPAccount( $_, $tab1, $align25 );
        }
        elsif ( $dictVal eq 'IMAPAccount' ) {
            parseIMAPAccount( $_, $tab1, $align25 );
        }
        else {
            ::rptMsg( $tab1."not managed by plugin!" );
        }
        ::rptMsg("");
    }
    ::rptMsg("");
}
    
#------------------------------------------------------------------------------
# dict parsers
#------------------------------------------------------------------------------

sub parseDeviceLocalAccount
{
    my $plist = shift;
    my $tab = shift;
    my $align = shift;
    
    my $key; my $subkey; my $val;

    $key = 'Identifier';
    $val = ::getPlistKeyValue( $plist, $key );
    $val = 'Not found' if not defined $val;
    ::rptMsg(sprintf($tab."$align = %s", $key, $val));
    
    $key = 'Type';
    $val = ::getPlistKeyValue( $plist, $key );
    $val = 'Not found' if not defined $val;
    ::rptMsg(sprintf($tab."$align = %s", $key, $val));
    
    $key = 'Enabled Dataclasses';
    $subkey = ::getPlistKeyValue( $plist, $key );
    if ( not defined $subkey ) {
        ::rptMsg(sprintf( $tab."$align = %s", $key, 'Not found'));
    }
    else {
        ::rptMsg(sprintf($tab."$align", $key));
        ::rptMsg($tab.$tab1.$_) for (@{$subkey});
    }
}

#------------------------------------------------------------------------------

sub parseLocalAccount
{
    my $plist = shift;
    my $tab = shift;
    my $align = shift;
    
    my $key; my $subkey; my $val;

    $key = 'Type';
    $val = ::getPlistKeyValue( $plist, $key );
    $val = 'Not found' if not defined $val;
    ::rptMsg(sprintf($tab."$align = %s", $key, $val));
    
    $key = 'AccountPath';
    $val = ::getPlistKeyValue( $plist, $key );
    $val = 'Not found' if not defined $val;
    ::rptMsg(sprintf($tab."$align = %s", $key, $val));
    
    $key = 'SentMessagesMailboxName';
    $val = ::getPlistKeyValue( $plist, $key );
    $val = 'Not found' if not defined $val;
    ::rptMsg(sprintf($tab."$align = %s", $key, $val));

    $key = 'TrashMailboxName';
    $val = ::getPlistKeyValue( $plist, $key );
    $val = 'Not found' if not defined $val;
    ::rptMsg(sprintf($tab."$align = %s", $key, $val));
    
    $key = 'Identifier';
    $val = ::getPlistKeyValue( $plist, $key );
    $val = 'Not found' if not defined $val;
    ::rptMsg(sprintf($tab."$align = %s", $key, $val));
   
    $key = 'Enabled Dataclasses';
    $subkey = ::getPlistKeyValue( $plist, $key );
    if ( not defined $subkey ) {
        ::rptMsg(sprintf( $tab."$align = %s", $key, 'Not found'));
    }
    else {
        ::rptMsg(sprintf($tab."$align", $key));
        ::rptMsg($tab.$tab1.$_) for (@{$subkey});
    }
}

#------------------------------------------------------------------------------

sub parseSMTPAccount
{
    my $plist = shift;
    my $tab = shift;
    my $align = shift;
    
    my $key; my $subkey; my $val;

    $key = 'Type';
    $val = ::getPlistKeyValue( $plist, $key );
    $val = 'Not found' if not defined $val;
    ::rptMsg(sprintf($tab."$align = %s", $key, $val));
    
    $key = 'Username';
    $val = ::getPlistKeyValue( $plist, $key );
    $val = 'Not found' if not defined $val;
    ::rptMsg(sprintf($tab."$align = %s", $key, $val));
    
    $key = 'Hostname';
    $val = ::getPlistKeyValue( $plist, $key );
    $val = 'Not found' if not defined $val;
    ::rptMsg(sprintf($tab."$align = %s", $key, $val));

    $key = 'ShouldUseAuthentication';
    $val = ::getPlistKeyValue( $plist, $key );
    $val = 'Not found' if not defined $val;
    ::rptMsg(sprintf($tab."$align = %s", $key, $val));

    $key = 'Identifier';
    $val = ::getPlistKeyValue( $plist, $key );
    $val = 'Not found' if not defined $val;
    ::rptMsg(sprintf($tab."$align = %s", $key, $val));
    
    $key = 'Sync Identifier';
    $val = ::getPlistKeyValue( $plist, $key );
    $val = 'Not found' if not defined $val;
    ::rptMsg(sprintf($tab."$align = %s", $key, $val));
}

#------------------------------------------------------------------------------

sub parseIMAPAccount
{
    my $plist = shift;
    my $tab = shift;
    my $align = shift;
    
    my $key; my $subkey; my $val;

    $key = 'Type';
    $val = ::getPlistKeyValue( $plist, $key );
    $val = 'Not found' if not defined $val;
    ::rptMsg(sprintf($tab."$align = %s", $key, $val));
    
    $key = 'Username';
    $val = ::getPlistKeyValue( $plist, $key );
    $val = 'Not found' if not defined $val;
    ::rptMsg(sprintf($tab."$align = %s", $key, $val));
    
    $key = 'FullUserName';
    $val = ::getPlistKeyValue( $plist, $key );
    $val = 'Not found' if not defined $val;
    ::rptMsg(sprintf($tab."$align = %s", $key, $val));
    
    $key = 'Hostname';
    $val = ::getPlistKeyValue( $plist, $key );
    $val = 'Not found' if not defined $val;
    ::rptMsg(sprintf($tab."$align = %s", $key, $val));

    $key = 'DisplayName';
    $val = ::getPlistKeyValue( $plist, $key );
    $val = 'Not found' if not defined $val;
    ::rptMsg(sprintf($tab."$align = %s", $key, $val));
    
    $key = 'AccountPath';
    $val = ::getPlistKeyValue( $plist, $key );
    $val = 'Not found' if not defined $val;
    ::rptMsg(sprintf($tab."$align = %s", $key, $val));
        
    $key = 'SentMessagesMailboxName';
    $val = ::getPlistKeyValue( $plist, $key );
    $val = 'Not found' if not defined $val;
    ::rptMsg(sprintf($tab."$align = %s", $key, $val));

    $key = 'TrashMailboxName';
    $val = ::getPlistKeyValue( $plist, $key );
    $val = 'Not found' if not defined $val;
    ::rptMsg(sprintf($tab."$align = %s", $key, $val));
    
    $key = 'NumberOfDaysToKeepTrash';
    $val = ::getPlistKeyValue( $plist, $key );
    $val = 'Not found' if not defined $val;
    ::rptMsg(sprintf($tab."$align = %s", $key, $val));

    $key = 'SSLEnabled';
    $val = ::getPlistKeyValue( $plist, $key );
    $val = 'Not found' if not defined $val;
    ::rptMsg(sprintf($tab."$align = %s", $key, $val));
    
    $key = 'StoreDraftsOnServer';
    $val = ::getPlistKeyValue( $plist, $key );
    $val = 'Not found' if not defined $val;
    ::rptMsg(sprintf($tab."$align = %s", $key, $val));
    
    $key = 'StoreSentMessagesOnServer';
    $val = ::getPlistKeyValue( $plist, $key );
    $val = 'Not found' if not defined $val;
    ::rptMsg(sprintf($tab."$align = %s", $key, $val));
    
    $key = 'StoreTrashOnServer';
    $val = ::getPlistKeyValue( $plist, $key );
    $val = 'Not found' if not defined $val;
    ::rptMsg(sprintf($tab."$align = %s", $key, $val));
    
    $key = 'Identifier';
    $val = ::getPlistKeyValue( $plist, $key );
    $val = 'Not found' if not defined $val;
    ::rptMsg(sprintf($tab."$align = %s", $key, $val));
    
    $key = 'Sync Identifier';
    $val = ::getPlistKeyValue( $plist, $key );
    $val = 'Not found' if not defined $val;
    ::rptMsg(sprintf($tab."$align = %s", $key, $val));
    
    $key = 'EmailAddresses';
    $subkey = ::getPlistKeyValue( $plist, $key );
    if ( not defined $subkey ) {
        ::rptMsg(sprintf( $tab."$align = %s", $key, 'Not found'));
    }
    else {
        ::rptMsg(sprintf($tab."$align", $key));
        ::rptMsg($tab.$tab1.$_) for (@{$subkey});
    }
    
    $key = 'Enabled Dataclasses';
    $subkey = ::getPlistKeyValue( $plist, $key );
    if ( not defined $subkey ) {
        ::rptMsg(sprintf( $tab."$align = %s", $key, 'Not found'));
    }
    else {
        ::rptMsg(sprintf($tab."$align", $key));
        ::rptMsg($tab.$tab1.$_) for (@{$subkey});
    }
}

#------------------------------------------------------------------------------
1;